/*
 * ServerConfigDialog.java
 *
 * Created on 12 de agosto de 2008, 23:15
 */

package l2bot.interfaz.dialogs;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import l2bot.Idioma;
import org.w3c.dom.*;
import org.xml.sax.SAXException;



/**
 *
 * @author  carl
 */
public class ServerConfigDialog extends javax.swing.JDialog {

    /** Creates new form ServerConfigDialog */
    java.awt.Frame par;
    NodeList f;
    
    public ServerConfigDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        par = parent;
  
        

        
        initComponents();
        servers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        list1 = new java.awt.List();

        setTitle("Servidores");

        jButton1.setText(Idioma.aceptar);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setText(Idioma.borrar);
        jButton4.setPreferredSize(new java.awt.Dimension(71, 23));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setText(Idioma.cargar);
        jButton3.setMaximumSize(new java.awt.Dimension(71, 23));
        jButton3.setMinimumSize(new java.awt.Dimension(71, 23));
        jButton3.setPreferredSize(new java.awt.Dimension(71, 23));

        jButton2.setText(Idioma.nuevo);
        jButton2.setPreferredSize(new java.awt.Dimension(71, 23));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton5.setText(Idioma.editar);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton5)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(list1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    if(list1.getSelectedIndex() < 0){
    JOptionPane.showMessageDialog( this,"Tiene que seleccionar un servidor");
    return;
    }
    if(JOptionPane.showConfirmDialog( this,"¿Estas seguro?","confirmacion",JOptionPane.YES_NO_OPTION ) == JOptionPane.NO_OPTION){
        return;
    }
    
    try{
        FileWriter w=new FileWriter("xml/servers.xml");
        w.write("<servers>\n");
        for(int i=0;i<f.getLength();i++){
            Element h = (Element) f.item(i);
             if(!list1.getSelectedItem().equals(h.getAttribute("name"))){
                w.write("     <server name='"+h.getAttribute("name")+"' host='"+h.getAttribute("host")+"' port='"+h.getAttribute("port")+"' gameservers='"+h.getAttribute("gameservers")+"'/>\n");
             }
        }
        w.write("</servers>\n");
        w.flush();
        w.close();
        list1.remove(list1.getSelectedIndex());
    }catch(IOException e)
    {}
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    if(list1.getSelectedIndex() < 0){
        JOptionPane.showMessageDialog( this,"Tiene que seleccionar un servidor");
        return;
    }
    ServerEdit gf = new ServerEdit(par,true,list1.getSelectedItem());
    gf.setVisible(true);
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    ServerEdit gf = new ServerEdit(par,true,null);
    gf.setVisible(true);
    servers();
}//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    
    
    public void servers(){
        
        parsear();
        
        //System.out.print(((Element)f.item(0)).getAttribute("host"));
        list1.removeAll();
        for(int i=0;i<f.getLength();i++){
            Element h = (Element)f.item(i);
            //System.out.print(h.getAttribute("id"));
            list1.add(h.getAttribute("name"));
            list1.select(i);
        }
        
    }
    
    public void parsear(){
        DOMParser parser = new DOMParser();
        try {
            parser.parse("xml/servers.xml");
        } catch (SAXException ex) {
            Logger.getLogger(ServerConfigDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServerConfigDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        Document doc = parser.getDocument();
        f = doc.getElementsByTagName("server");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private java.awt.List list1;
    // End of variables declaration//GEN-END:variables

}
